package com.resou.reader.mine.presenter;

import android.widget.Toast;

import com.resou.reader.ReSouApplication;
import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.entry.UserInfoBean;
import com.resou.reader.api.service.UserService;
import com.resou.reader.base.m.IModel;
import com.resou.reader.base.p.BasePresenter;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.eventbus.RefreshStoreEvent;
import com.resou.reader.utils.eventbus.UpdateUserIconEvent;
import com.resou.reader.utils.log.DLog;
import com.resou.reader.mine.IView.IUserInfoView;
import com.resou.reader.mine.IView.UserInstance;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by ${Garrett} on 2018/10/1.
 * Contact me krouky@outlook.com
 */
public class UserInfoPresenter extends BasePresenter<IUserInfoView, IModel> {

    private static final String TAG = "UserInfoPresenter";
    private UserService mService;

    public UserInfoPresenter(IUserInfoView view) {
        this.view = view;
        mService = ApiImp.getInstance().getService(UserService.class);
    }

    public void updateHead(final File file) {
        RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("fileName", file.getName(), requestFile);

        mService.updateUserIcon(UserInstance.getToken(), body).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .as(this.<Result>bindLifecycle())
                .subscribe(new Consumer<Result>() {
                    @Override
                    public void accept(Result result) throws Exception {
                        //通知其他界面更新
                        UpdateUserIconEvent event = new UpdateUserIconEvent();
                        event.isUpdateUserIcon = true;
                        EventBus.getDefault().post(event);
                        DLog.i(TAG, "updateUserIcon success : code");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DLog.i(TAG, "updateUserIcon error, msg :" + throwable.getMessage());
                        Toast.makeText(ReSouApplication.getRSApplication().getApplicationContext(), "头像上传失败", Toast.LENGTH_SHORT).show();
                        throwable.printStackTrace();
                    }
                });
    }

    public void updateNickname(final String nickname) {
        mService.updateNickname(UserInstance.getToken(), nickname).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .as(this.<Result>bindLifecycle())
                .subscribe(new Consumer<Result>() {
                    @Override
                    public void accept(Result result) throws Exception {
                        int code = result.getCode();
                        DLog.i(TAG, "updateNickname success : code == " + code);
                        if (code == 0) {
                            view.nicknameUpdate(nickname);
                            UserInstance.getLoginData().setNickName(nickname);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DLog.i(TAG, "updateNickname error, msg :" + throwable.getMessage());
                        throwable.printStackTrace();
                        ToastUtil.makeShortToast("数据异常，请重试");
                    }
                });
    }

    public void updateSign(String sign) {

    }

    public void updateGender(final String gender) {
        mService.updateUserGender(UserInstance.getToken(), gender)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .as(this.<Result<UserInfoBean>>bindLifecycle())
                .subscribe(new Consumer<Result<UserInfoBean>>() {
                    @Override
                    public void accept(Result<UserInfoBean> userInfoBeanResult) {
                        int code = userInfoBeanResult.getCode();
                        DLog.i(TAG, "updateNickname success : code == " + code);
                        if (code == 0) {
//                            view.birthdayUpdate(birthday);
                            view.genderUpdate(gender);
                            UserInstance.getLoginData().setGender(gender);
                            EventBus.getDefault().post(new RefreshStoreEvent());

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        DLog.i(TAG, "updateGender error, msg :" + throwable.getMessage());
                        throwable.printStackTrace();
                    }
                });
    }
}
